package messages;

import controllers.DBConnection;
import ocsf.server.ConnectionToClient;

public class UpdatePricesMessage {

	ConnectionToClient client;
	String[] arr;
	double sample,picture,layer,time,pac;
	/**
	 * update prices message by connecting with DB
	 * @param strMsg
	 * @param client
	 */
	
	public UpdatePricesMessage(String[] strMsg, ConnectionToClient client){
		
		this.client=client;
		this.arr=strMsg;
		
				//get new prices and convert from String to Double
				this.sample=Double.parseDouble(arr[1]);
				this.picture=Double.parseDouble(arr[2]);
				this.layer=Double.parseDouble(arr[3]);
				this.time=Double.parseDouble(arr[4]);
				this.pac=Double.parseDouble(arr[5]);
	}
	
	
	
	public void activateUpdate(){
		
		try{	
			/**
			 * making a connection with DB
			 */
			DBConnection conn=new DBConnection();
			
			if(!arr[1].equals("")){
				java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET price=? ,newPrice=? WHERE ID=? ");
	            pr1.setDouble(1,sample);
	            pr1.setDouble(2,0);
	            pr1.setString(3,"101");
				pr1.executeUpdate();	
				pr1.close();
			}
			if(!arr[2].equals("")){
				java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET price=? ,newPrice=? WHERE ID=? ");
	            pr1.setDouble(1,picture);
	            pr1.setDouble(2,0);
	            pr1.setString(3,"102");
				pr1.executeUpdate();	
				pr1.close();
			}
			if(!arr[3].equals("")){
				java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET price=? ,newPrice=? WHERE ID=? ");
	            pr1.setDouble(1,layer);
	            pr1.setDouble(2,0);
	            pr1.setString(3,"103");
				pr1.executeUpdate();	
				pr1.close();
			}
			if(!arr[4].equals("")){
				java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET price=? ,newPrice=? WHERE ID=? ");
	            pr1.setDouble(1,time);
	            pr1.setDouble(2,0);
	            pr1.setString(3,"104");
				pr1.executeUpdate();	
				pr1.close();
			}
			if(!arr[5].equals("")){
				java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET price=? ,newPrice=? WHERE ID=? ");
	            pr1.setDouble(1,pac);
	            pr1.setDouble(2,0);
	            pr1.setString(3,"105");
				pr1.executeUpdate();	
				pr1.close();
			}
		
			System.out.println("Update Prices is done");
			client.sendToClient("UpdatePrices");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
	
}
